/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.antColonyOptimisation.ant.pathFinder;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

import org.utc.ia04.antsontheweb.utilities.Requete;

import jade.core.AID;
import jade.core.Agent;
import jade.lang.acl.ACLMessage;

public class AntPathFinderAgent extends Agent 
{
	//serialVersionUID
	private static final long serialVersionUID = 1427756180403947510L;

	//attributes
	private AID parserAID;
	private AID antAID;
	private URL antColonyUrl;
	private Stack<URL> visitedUrl;
	private Set<URL> deadEndsUrl;
	private boolean myAntIsDead = false;

	public void setup()
	{
		Object[] args = getArguments();
		setAntAID(new AID( args[0].toString(),true));
		try {
			antColonyUrl =new URL( args[1].toString());
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}
		parserAID = null;
		visitedUrl = new Stack<URL>();
		visitedUrl.push(antColonyUrl);
		deadEndsUrl = new HashSet<URL>();
		addBehaviour(new GetParserAIDBhv());
	}
	
	public void setMyAntIsDead() {
		myAntIsDead = true;
	}

	public void setParserAID(AID parserAID) {
		this.parserAID = parserAID;
	}

	public AID getParserAID() {
		return parserAID;
	}

	public void setAntAID(AID antAID) {
		this.antAID = antAID;
	}

	public AID getAntAID() {
		return antAID;
	}

	public void setAntColonyUrl(URL antColonyURl) {
		this.antColonyUrl = antColonyURl;
	}

	public URL getAntColonyUrl() {
		return antColonyUrl;
	}


	public void setVisitedUrl(Stack<URL> visitedUrl) {
		this.visitedUrl = visitedUrl;
	}

	public Stack<URL> getVisitedUrl() {
		return visitedUrl;
	}

	public void setDeadEndsUrl(Set<URL> deadEndsUrl) {
		this.deadEndsUrl = deadEndsUrl;
	}

	public Set<URL> getDeadEndsUrl() {
		return deadEndsUrl;
	}
	
	public void takeDown()
	{
		if(!myAntIsDead)
		{
			ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
			message.addReceiver(getAntAID());
			Requete req = new Requete(Requete.AC_DEATH, null);
			message.setContent(req.serialize());
			send(message);
		}
	}
}
